$(function(){
	$("#orderImport").on('click', function(){
		var batchId = $('#batchId').val();
		if(!batchId){
			eDialog.alert('尚未导入订单');
			return;
		}
		$.ajax({
			url: GLOBAL.WEBROOT+"/order_info/save_order",
	        data:{
	        	batchId:batchId
	        },
	        type:"post",
	        dataType:"json",
	        async:true,
		    cache:true,
	        success: function (data){
	        	querySuccessData(1);
	        },
	        error:function(XMLHttpRequest, textStatus, errorThrown){
	        	
	        }
		});
	});
	$(document).on('change', '#orderFile', function(){
		var orderFile = $(this).val();
		if(orderFile){			
			var url = WEB_ROOT+"/order_info/order_import";
			$.ajaxFileUpload({
				url :url ,
				secureuri : false,
				fileElementId : 'orderFile',
				type : 'POST',
				dataType : 'json', //or json xml whatever you like~
				success : function(data, status) {
					var file = $("#orderFile") 
					file.after(file.clone().val("")); 
					file.remove();
					console.info(data);
					if (data.flag) {
						$('#batchId').val(data.msg);
						queryOrderData(1);
					} else {
						eDialog.alert(data.msg);
					}
				},
				error : function(data, status, e) {
					//$("#result").append(data);
					console.log("error");
				}
			});
		}else{
			eDialog.alert('请先选择文件');
			return;
		}
	});
	$("#viewSuccess").on('click', function(){
		querySuccessData(1);
	});
	$("#viewFail").on('click', function(){
		queryFailData(1);
	});
	$("#backBtn").on('click', function(){
		window.top.location.href =WEB_ROOT+"/order_info/order_init"
	});
});

function queryOrderData(pageNo){
    var params = {
    	batchId:$.trim($("#batchId").val()),
        pageNo:pageNo,
        pageSize:50
    };

    ajaxLoadPage(params,WEB_ROOT+"/order_info/order_import_list",$("#orderlist-div-id"),function(){
        $("#urlPagerId").pager({callback:function(pageNo){
        	queryOrderData(pageNo);
        }});
    });
}

function querySuccessData(pageNo){
    var params = {
        batchId:$.trim($("#batchId").val()),
        state:'1',
        pageNo:pageNo,
        pageSize:50
    };

    ajaxLoadPage(params,WEB_ROOT+"/order_info/order_import_success_list",$("#orderlist-div-id"),function(){
        $("#urlPagerId").pager({callback:function(pageNo){
          	querySuccessData(pageNo);
        }});
    });
}

function queryFailData(pageNo){
    var params = {
        batchId:$.trim($("#batchId").val()),
        state:'2',
        pageNo:pageNo,
        pageSize:50
    };

    ajaxLoadPage(params,WEB_ROOT+"/order_info/order_import_fail_list",$("#orderlist-div-id"),function(){
        $("#urlPagerId").pager({callback:function(pageNo){
        	queryFailData(pageNo);
        }});
    });
}